package com.swingolf.dao.hrd;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.swingolf.dao.PlayerDAO;
import com.swingolf.dao.TournamentDAO;
import com.swingolf.model.Level;
import com.swingolf.model.Player;
import com.swingolf.model.Team;
import com.swingolf.model.Tournament;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * The <code>HRDTemperatureDAOImpl</code> provides crud methods for accessing collecting temperatures.
 */
@Repository
public class HRDPlayerDAOImpl implements PlayerDAO {

  @Autowired HRDTeamDAOImpl teamDAO;

  @Autowired TournamentDAO tournamentDAO;

  public void delete(Player player) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.delete(KeyFactory.createKey("Player", player.getId()));
  }

  @Override
  public void update(Player player) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity temperatureEntity = null;
    try {
      temperatureEntity = datastore.get(KeyFactory.createKey("Player", player.getId()));
    } catch (EntityNotFoundException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
    temperatureEntity.setProperty("Name", player.getName());
    temperatureEntity.setProperty("Score", player.getScore());
    temperatureEntity.setProperty("TeamId", player.getTeam() == null ? null : player.getTeam().getId());
    temperatureEntity.setProperty("ImageId", player.getImageId());
    temperatureEntity.setProperty("Level", player.getLevel() == null ? null : player.getLevel().toString());
    temperatureEntity.setProperty("Tournament", player.getTournament() == null ? null : player.getTournament().getId());
    Key key = datastore.put(temperatureEntity);
  }

  @Override
  public void save(Player player) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity temperatureEntity = new Entity("Player");
    temperatureEntity.setProperty("Name", player.getName());
    temperatureEntity.setProperty("Score", player.getScore());
    temperatureEntity.setProperty("TeamId", player.getTeam() == null ? null : player.getTeam().getId());
    temperatureEntity.setProperty("ImageId", player.getImageId());
    temperatureEntity.setProperty("Level", player.getLevel() == null ? null : player.getLevel().toString());
    temperatureEntity.setProperty("Tournament", player.getTournament() == null ? null : player.getTournament().getId());
    Key key = datastore.put(temperatureEntity);
    player.setId(key.getId());
  }

  @Override
  public Player findById(Long id) {
    return null;
  }

  @Override
  public List<Player> findAll() {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query q = new Query("Player");
    q.addSort("Name", Query.SortDirection.ASCENDING);
    PreparedQuery pq = datastore.prepare(q);
    List<Player> result = new LinkedList<Player>();
    Iterable<Entity> iterable = pq.asIterable();
    //for (Entity entity : iterable) {
    //  datastore.delete(entity.getKey());
    //}
    for (Entity entity : iterable) {
      Long imageId = ((Long) entity.getProperty("ImageId"));
      Long teamId = ((Long) entity.getProperty("TeamId"));
      Team team = teamId == null ? null : teamDAO.findById(teamId);
      String levelString = (String) entity.getProperty("Level");
      Level level = levelString == null ? null : Level.valueOf(levelString);

      Long tournamentString = (Long) entity.getProperty("Tournament");
      Tournament tournament = tournamentString == null ? null : tournamentDAO.findById(tournamentString);


      result.add(new Player(entity.getKey().getId(), (String) entity.getProperty("Name"), ((Long) entity.getProperty("Score")).intValue(), imageId, team, level, tournament));
    }
    return result;

  }

  private void deleteAll() {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  }
}
